﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormReportChiTietPhieuNhap : Form
    {
        private Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();
        public FormLapPhieuNhap frmThongkeCTPN;
        //private List<Db.ChitietNhapXuat> listNhapXuat;
        public FormReportChiTietPhieuNhap()
        {
            InitializeComponent();
        }

        public FormReportChiTietPhieuNhap(FormLapPhieuNhap frm)
        {
            InitializeComponent();
            frmThongkeCTPN = frm;
        }
        string sophieunhap;
        public FormReportChiTietPhieuNhap(FormLapPhieuNhap frm,string _sophieunhap)
        {
            InitializeComponent();
            frmThongkeCTPN = frm;
            this.sophieunhap = _sophieunhap;
        }


        private void FormReportChiTietPhieuNhap_Load(object sender, EventArgs e)
        {
            if (this.frmThongkeCTPN != null && !string.IsNullOrEmpty(sophieunhap))
            {
                var ct = (from o in db.PHIEUNHAPs
                          where o.SoPhieuNhap == sophieunhap
                          select new Db.ThongKeChiTietPhieuNhap { 
                          TongTien=o.TongTien.ToString(),
                          TienDaTra=o.TienDaTra.ToString(),
                          TenNhanVien=o.NHANVIEN.TenNhanVien,
                          SoPhieuNhap=o.SoPhieuNhap,
                          NhaCungCap=o.NHACUNGCAP.TenNhaCungCap,
                          NgayLapPhieu=o.NgayLap.ToString(),
                          ConLai=(o.TongTien-o.TienDaTra).ToString(),
                          ChiTietNhap=(from k in o.CHITIETPHIEUNHAPs
                                       select new Db.ChitietNhapXuat{
                                       DonGia=k.DonGiaNhap??0,
                                       GiaNhap=k.DonGiaNhap??0,
                                       MaChiTiet=k.SoPhieuNhap,
                                       MaHang=k.MaHang,
                                       TenHang=k.MATHANG.TenHang,
                                       SoLuong=k.SoLuongNhap??0                                     
                                       
                                       
                                       }).ToList()                          
                          
                          }).ToList();
                //var ct = (from o in listNhapXuat
                //          select new Db.ThongKeChiTietPhieuNhap {
                //        //  TongTien=o.
                          
                //          }).ToList();
               //this.BindingSource.DataSource = ct;
                this.ThongKeChiTietPhieuNhapBindingSource.DataSource = ct;
                this.ChitietNhapXuatBindingSource.DataSource = ct.First().ChiTietNhap;
                this.reportViewer1.RefreshReport();
                return;

            }
            else
            {
                var chitietphieunhap = (from m in db.CHITIETPHIEUNHAPs

                                        select new Db.Chitietchungtuthongke
                                        {

                                            NgayLap = m.PHIEUNHAP.NgayLap ?? DateTime.Now,//neu ngay lap ma null thi lay ngay hien tai                                 
                                            SoTienDaTra = m.PHIEUNHAP.TienDaTra ?? 0,
                                            TenNhanVien = m.PHIEUNHAP.NHANVIEN.TenNhanVien,
                                            TongSoTien = m.PHIEUNHAP.TongTien ?? 0,
                                            TenNhaCungCap = m.PHIEUNHAP.NHACUNGCAP.TenNhaCungCap,
                                            HanThanhToan = m.PHIEUNHAP.HanThanhToan ?? DateTime.Now,
                                            MaHang = m.MaHang,
                                            TienDaTra = m.PHIEUNHAP.TienDaTra ?? 0,
                                            SoPhieuNhap = m.SoPhieuNhap,
                                            TenHang = m.MATHANG.TenHang,
                                            SoLuongTon = m.SoLuongNhap ?? 0,
                                            GiaNhap = m.DonGiaNhap ?? 0

                                        }).ToList();
                this.BindingSource.DataSource = chitietphieunhap;
                this.reportViewer1.RefreshReport();
            }
        }
    }
}
